<?php

namespace backend\models;

use Yii;
use yii\db\Query;

/**
 * This is the model class for table "basepic".
 *
 * @property int $id
 * @property string $pic_name 图片描述
 * @property string $pic_url 图片地址
 * @property string $insert_time 插入时间
 * @property int $status 是否显示 0 可以 1 不可以
 * @property int $sort 排序
 */
class Basepic extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'basepic';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['insert_time'], 'safe'],
            [['status', 'sort'], 'integer'],
            [['pic_name', 'pic_url'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'pic_name' => 'Pic Name',
            'pic_url' => 'Pic Url',
            'insert_time' => 'Insert Time',
            'status' => 'Status',
            'sort' => 'Sort',
        ];
    }


    public function getPicData($post, $page, $pageSize)
    {
        $offset = max(0, ($page-1))*$pageSize;
        $s = isset($post['s']) ? $post['s'] : '';
        $f = isset($post['f']) ? $post['f'] : '';
        $query = (new Query())->select('*')
            ->from('basepic')
            ->andFilterWhere(['between', 'insert_time', $s, $f])
            ->orderBy('id asc');
        $data = $query->offset($offset)->limit($pageSize)->all();
        foreach ($data as $key=>$value){
            if (!empty($value['pic_url'])){
                $picUrl = explode('uploaded', $value['pic_url']);
                $data[$key]['pic_url'] = $picUrl[1];
            }else{
                $data[$key]['pic_url'] = '图片地址丢失，注意重新上传';
            }
        }
        return ['data' => $data, 'limit'=> intval($pageSize), 'page'=> intval($page), 'statusCode'=>200, 'total'=> $query->count()];
    }

    public function insertData($post)
    {
        $db = yii::$app->db;
        $transaction  = $db->beginTransaction();
        try {
            $pic = new Basepic();
            $pic->pic_name = $post['pic_name'];
            $pic->pic_url = $post['picImage'];
            $pic->insert_time = $post['insert_time'];
            $pic->status = isset($post['status']) ? 0 : 1;
            $pic->sort = $post['sort'];
            $pic->save();
            $transaction->commit();
            return ['statusCode' => 200, 'message' => '首页轮播图提交成功'];
        }catch (\Exception $e){
            $transaction->rollBack();
            return ['statusCode' => 300, 'message' => '首页轮播图提交失败'];
        }
    }

    public function updateData($post)
    {
        $db = yii::$app->db;
        $transaction  = $db->beginTransaction();
        try {
            $pic = self::findOne($post['id']);
            $pic->pic_name = $post['pic_name'];
            $pic->pic_url = $post['picImage'];
            $pic->insert_time = $post['insert_time'];
            $pic->status = isset($post['status']) ? 0 : 1;
            $pic->sort = $post['sort'];
            $pic->save();
            $transaction->commit();
            return ['statusCode' => 200, 'message' => '首页轮播图修改成功'];
        }catch (\Exception $e){
            $transaction->rollBack();
            return ['statusCode' => 300, 'message' => $e->getMessage()];
        }
    }

    public function deleteData($post)
    {
        $ids = explode(',', $post['ids']);
        foreach ($ids as $id){
            if ($id == 1 || $id == 2 || $id == 3 || $id == 4){
                return ['statusCode' => 300, 'message' => '包含禁止删除的项目'];
                break;
            }
        }
        $result = self::deleteAll(['in', 'id', $ids]);
        if ($result){
            return ['statusCode' => 200, 'message' => '删除成功'];
        }
        return ['statusCode' => 300, 'message' => '删除失败'];
    }
}
